﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Pay.Common;
using Pay.Http.Utilitys;
using Pay.Window.Data.Dto;

namespace Pay.Window.Data
{
    public partial class IndexForm : Form
    {
        public IndexForm()
        {
            InitializeComponent();
        }

        private void IndexForm_Load(object sender, EventArgs e)
        {
            IndexForm_Init();
        }

        private void IndexForm_Init()
        {
        }

        private async void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbLogin.Text))
            {
                MessageBox.Show("请输入系统账号");
                return;
            }

            var url = "http://api.pay.com/api/aliService/Accountinfo/{0}";

            var resultData = new
            {
                success = false,
                data = new
                {
                    account = string.Empty,
                    pwd = string.Empty
                }
            };

            resultData = await HttpClientHelper.GetAnonymousType(string.Format(url, tbLogin.Text), resultData);

            if (resultData == null || resultData.data == null || string.IsNullOrWhiteSpace(resultData.data.account))
            {
                MessageBox.Show("没有找到用户");
                return;
            }

            var pwd = string.Empty;
            try
            {
                pwd = PasswordHelper.Decoding(resultData.data.pwd, 3);
            }
            catch
            {
                MessageBox.Show("数据解密失败");
                return;
            }

            var form = new DefaultForm(new WindowAlipayAccountOutput { AlipayAccount = resultData.data.account, AlipayPassword = pwd, SystemAccount = tbLogin.Text });
            form.StartPosition = FormStartPosition.CenterScreen;
            form.Show();
            this.Hide();
        }

        private void tbLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnLogin.PerformClick();
            }
        }
    }
}
